package love.yuanmengrobot.als.business.servlet;

import love.yuanmengrobot.als.business.dao.AcademicWarningDao;
import love.yuanmengrobot.als.business.dao.ImportPlainDao;
import love.yuanmengrobot.als.business.dao.StudentNotCourseDao;
import love.yuanmengrobot.als.business.dao.impl.*;
import love.yuanmengrobot.als.business.javaBean.Alstrainplan;
import love.yuanmengrobot.als.business.javaBean.Alstrainplancourse;
import love.yuanmengrobot.als.business.javaBean.Student;
import love.yuanmengrobot.als.business.javaBean.StudentViewCredit;
import love.yuanmengrobot.als.utilPublic.DoubleFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class academicWarningServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("GBK");
        response.setContentType("text/html;charset=GBK");
        String id = request.getParameter("studentNo");
        StudentViewCredit svc = null;
        if (id != null && id.length() > 0) {//验证账号长度
            FindStudentInfoDaoImpl findStudentInfoDaoImpl = new FindStudentInfoDaoImpl();
            AcademicWarningDao academicWarningDao = new AcademicWarningDaoImpl();
            svc = findStudentInfoDaoImpl.selectInfo(id);
            if (svc != null) {//账号存在
                StudentViewCredit temp = new StudentCreditDaoImpl().getStudentCredit(id);//获取已修学分
                svc.setLimitCreditSum(temp.getLimitCreditSum());//限选已修
                svc.setOptionCreditSum(temp.getOptionCreditSum());//任选已修
                svc.setTongShiCreditSum(temp.getTongShiCreditSum());//通识已修
                svc.setStudentCreditSum(temp.getStudentCreditSum());//已修学分
                request.setAttribute("warning", academicWarningDao.getWarning(id));//学业预警
                request.setAttribute("id", id);//学号
                request.setAttribute("stuNo", id);//学号
                request.setAttribute("name", svc.getStuName());
                request.setAttribute("limitCreditSum", svc.getLimitCreditSum());
                request.setAttribute("optionCreditSum", svc.getOptionCreditSum());
                request.setAttribute("tongShiCreditSum", svc.getTongShiCreditSum());
                request.setAttribute("creditSum", svc.getStudentCreditSum());

                Alstrainplan trainPlan = new GetTrainPlanImpl().getTrainPlan(svc.getMajorId());//获取该学生对应培养方案信息
//                System.out.println("AAAAAAAAAAAAAAAAAA"+svc.getMajorId());
                double trainPlanCreditSum = Double.parseDouble(trainPlan.getLimiteCredit()) + Double.parseDouble(trainPlan.getDisciplineBasis()) +
                        Double.parseDouble(trainPlan.getGeneralRequired()) + Double.parseDouble(trainPlan.getProfessionalRequired()) + Double.parseDouble(trainPlan.getGeneralElective()) +
                        Double.parseDouble(trainPlan.getOptionalCredit()) + Double.parseDouble(trainPlan.getPracticeLink());//获取应修总学分
                request.setAttribute("allCreditSum", DoubleFormat.format(trainPlanCreditSum));
                request.setAttribute("limitSum", trainPlan.getLimiteCredit());//限选应修
                request.setAttribute("optionSum", trainPlan.getOptionalCredit());//任选应修
                request.setAttribute("tongShiSum", trainPlan.getGeneralElective());//通识选修应修


                //查询应修课程
                HashMap<String, Integer> hashMap = new StudentGraduationCourseDaoImpl().getGraduationCourse(id);
                svc.setCourseMap(hashMap);
                //查询未修课程
                StudentNotCourseDaoImpl studentNotCourseDao = new StudentNotCourseDaoImpl();
                HashMap<String, Integer> coursesMap = studentNotCourseDao.getNotCourse(id);


                //获取挂科课程
                HashMap<String, Integer> hashFaMap = new HashMap<String, Integer>();
                for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                    if (entry.getValue() == 2) {
                        hashFaMap.put(entry.getKey(), entry.getValue());
                    }
                }
                request.setAttribute("hashMap", svc.getCourseMap());
                request.setAttribute("coursesMap", coursesMap);
                request.setAttribute("hashFaMap", hashFaMap);


                //设置进度条
                ImportPlainDao importPlainDao = new ImportPlainDaoImpl();
                ArrayList<Alstrainplancourse> als = importPlainDao.selectAllAlstrainplancourse(svc.getMajorId(), svc.getGrade());
                float plan_01 = 0;//大一
                float plan_02 = 0;//大二
                float plan_03 = 0;//大三
                float plan_04 = 0;//大四
                for (Alstrainplancourse a : als) {
                    if (a.getPlanCourseTerm() == null) continue;
                    if (a.getCredit() == null) continue;
                    int term = Integer.parseInt(a.getPlanCourseTerm());
                    if (term == 1 || term == 2) plan_01 += Float.parseFloat(a.getCredit());
                    if (term == 3 || term == 4) plan_02 += Float.parseFloat(a.getCredit());
                    if (term == 5 || term == 6) plan_03 += Float.parseFloat(a.getCredit());
                    if (term == 7 || term == 8) plan_04 += Float.parseFloat(a.getCredit());
                }
//                System.out.println("结果：" + plan_01 + ":" + plan_02 + ":" + plan_03 + ":" + plan_04);
                float sum = plan_01 + plan_02 + plan_03 + plan_04;
                float line_01 = plan_01 / sum;
                float line_02 = (plan_01 + plan_02) / sum;
                float line_03 = (plan_01 + plan_02 + plan_03) / sum;
                request.setAttribute("line_01", line_01);
                request.setAttribute("line_02", line_02);
                request.setAttribute("line_03", line_03);


            } else {
                request.setAttribute("id", "查找不到该学号");
                request.setAttribute("name", "");
            }
        }
        request.getRequestDispatcher("WEB-INF/view/business/academicWarning/academicWarning.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
